package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.StatusTitle;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.CurrencyMaster;
import data.DateTimeFormatMaster;
import data.LanguageMaster;
import data.PayModeMaster;

public class MasterCreator extends AbstractBL {
	
	public List<LanguageMaster> CreateLanguageMaster()
	{
		List<LanguageMaster> list = new ArrayList<LanguageMaster>();
		
		if(GetLanguage(StatusTitle.LanguageTitle.English) == null)
		{
			LanguageMaster eng = new LanguageMaster();
			eng.setTitle(StatusTitle.LanguageTitle.English);
			eng.setLanguageCode("en");
			eng.setLanguageSubCode("US");
			list.add(eng);
		}

		if(GetLanguage(StatusTitle.LanguageTitle.Thai) == null)
		{
			LanguageMaster th = new LanguageMaster();
			th.setTitle(StatusTitle.LanguageTitle.Thai);
			th.setLanguageCode("th");
			th.setLanguageSubCode("TH");
			list.add(th);
		}
		
		return list;
	}
	public LanguageMaster GetLanguage(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Language, id);
		try {
			Entity lang = datastore.get(key);
			LanguageMaster l = new LanguageMaster();
			l.setEntity(lang);
			return l;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public LanguageMaster GetLanguage(String languageTitle)
	{
		Filter filter = new FilterPredicate("title",FilterOperator.EQUAL,languageTitle);

		Query q = new Query(EntityName.Language).setFilter(filter);
		PreparedQuery pq = datastore.prepare(q);
		try{
			Entity e = pq.asSingleEntity();
			
			LanguageMaster l = new LanguageMaster();
			l.setEntity(e);
			
			return l;
		}
		catch(Exception e) { return null; }
	}
	
	public List<CurrencyMaster> CreateCurrencyMaster()
	{
		List<CurrencyMaster> list = new ArrayList<CurrencyMaster>();

		if(GetCurrency(StatusTitle.CurrencyTitle.THB) == null)
		{
			CurrencyMaster thb = new CurrencyMaster();
			thb.setTitle("Thailand, Baht");
			thb.setAbreviation(StatusTitle.CurrencyTitle.THB);
			thb.setSymbol("&#3647;");
			list.add(thb);
		}
		
		return list;
	}
	public CurrencyMaster GetCurrency(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Currency, id);
		try {
			Entity lang = datastore.get(key);
			CurrencyMaster c = new CurrencyMaster();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public CurrencyMaster GetCurrency(String currency)
	{
		Filter filter = new FilterPredicate("abreviation",FilterOperator.EQUAL,currency);

		Query q = new Query(EntityName.Currency).setFilter(filter);
		PreparedQuery pq = datastore.prepare(q);
		try{
			Entity e = pq.asSingleEntity();
			
			CurrencyMaster l = new CurrencyMaster();
			l.setEntity(e);
			
			return l;
		}
		catch(Exception e) { return null; }
	}
	
	public List<CurrencyMaster> GetAllCurrencyMaster()
	{
		Query q = new Query(EntityName.Currency);
		
		return GetCurrencyMasterList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<CurrencyMaster> GetCurrencyMasterList(List<Entity> entities)
	{
		List<CurrencyMaster> list = new ArrayList<CurrencyMaster>();
		for(Entity e : entities)
		{
			CurrencyMaster t = new CurrencyMaster();
			t.setEntity(e);
			list.add(t);
		}
		return list;
	}
	
	public List<LanguageMaster> GetAllLanguageMaster()
	{
		Query q = new Query(EntityName.Language);
		
		return GetLanguageMasterList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public List<LanguageMaster> GetLanguageMasterList(List<Entity> entities)
	{
		List<LanguageMaster> list = new ArrayList<LanguageMaster>();
		for(Entity e : entities)
		{
			LanguageMaster t = new LanguageMaster();
			t.setEntity(e);
			list.add(t);
		}
		return list;
	}
	
	public List<DateTimeFormatMaster> CreateDateTimeFormatMaster()
	{
		List<DateTimeFormatMaster> dateTimeFormat = new ArrayList<DateTimeFormatMaster>();
		DateTimeFormatMaster format;
		
		if(GetDateTimeFormat(StatusTitle.DateTimeFormatTitle.EEEEMMMMdy) == null)
		{
			format = new DateTimeFormatMaster();
			format.setDateTimeFormat(StatusTitle.DateTimeFormatTitle.EEEEMMMMdy);
			dateTimeFormat.add(format);
		}

		if(GetDateTimeFormat(StatusTitle.DateTimeFormatTitle.MMMMdy) == null)
		{
			format = new DateTimeFormatMaster();
			format.setDateTimeFormat(StatusTitle.DateTimeFormatTitle.MMMMdy);
			dateTimeFormat.add(format);
		}

		if(GetDateTimeFormat(StatusTitle.DateTimeFormatTitle.MMMdy) == null)
		{
			format = new DateTimeFormatMaster();
			format.setDateTimeFormat(StatusTitle.DateTimeFormatTitle.MMMdy);
			dateTimeFormat.add(format);
		}

		if(GetDateTimeFormat(StatusTitle.DateTimeFormatTitle.Mdyy) == null)
		{
			format = new DateTimeFormatMaster();
			format.setDateTimeFormat(StatusTitle.DateTimeFormatTitle.Mdyy);
			dateTimeFormat.add(format);
		}

		if(GetDateTimeFormat(StatusTitle.DateTimeFormatTitle.dMyy) == null)
		{
			format = new DateTimeFormatMaster();
			format.setDateTimeFormat(StatusTitle.DateTimeFormatTitle.dMyy);
			dateTimeFormat.add(format);
		}
		
		return dateTimeFormat;
	}
	public DateTimeFormatMaster GetDateTimeFormat(long id)
	{
		Key key = KeyFactory.createKey(EntityName.DateTimeFormat, id);
		try {
			Entity e = datastore.get(key);
			DateTimeFormatMaster m = new DateTimeFormatMaster();
			m.setEntity(e);
			return m;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public DateTimeFormatMaster GetDateTimeFormat(String format)
	{
		Filter filter = new FilterPredicate("dateTimeFormat",FilterOperator.EQUAL,format);

		Query q = new Query(EntityName.DateTimeFormat).setFilter(filter);
		PreparedQuery pq = datastore.prepare(q);
		try{
			Entity e = pq.asSingleEntity();
			
			DateTimeFormatMaster m = new DateTimeFormatMaster();
			m.setEntity(e);
			
			return m;
		}
		catch(Exception e) { return null; }
	}
	
public List<PayModeMaster> CreatePayModeMaster()
	{
		List<PayModeMaster> list = new ArrayList<PayModeMaster>();
		PayModeMaster paymode;
		
		if(GetPayModeMaster(StatusTitle.PayModeTitle.Cash) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.Cash);
			list.add(paymode);
		}
		if(GetPayModeMaster(StatusTitle.PayModeTitle.Cheque) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.Cheque);
			list.add(paymode);
		}
		if(GetPayModeMaster(StatusTitle.PayModeTitle.CreditCard) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.CreditCard);
			list.add(paymode);
		}
		if(GetPayModeMaster(StatusTitle.PayModeTitle.BankTransfer) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.BankTransfer);
			list.add(paymode);
		}
		if(GetPayModeMaster(StatusTitle.PayModeTitle.Others) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.Others);
			list.add(paymode);
		}
		if(GetPayModeMaster(StatusTitle.PayModeTitle.RoundOff) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.RoundOff);
			list.add(paymode);
		}
		if(GetPayModeMaster(StatusTitle.PayModeTitle.RevenueLoss) == null)
		{
			paymode = new PayModeMaster();
			paymode.setTitle(StatusTitle.PayModeTitle.RevenueLoss);
			list.add(paymode);
		}
		
		return list;
	}
	
	public PayModeMaster GetPayModeMaster(long id)
	{
		Key key = KeyFactory.createKey(EntityName.PayMode, id);
		try {
			Entity e = datastore.get(key);
			PayModeMaster p = new PayModeMaster();
			p.setEntity(e);
			return p;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	public PayModeMaster GetPayModeMaster(String title)
	{
		Filter filter = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Query q = new Query(EntityName.PayMode).setFilter(filter);
		PreparedQuery pq = datastore.prepare(q);
		try{
			Entity e = pq.asSingleEntity();
			
			PayModeMaster p = new PayModeMaster();
			p.setEntity(e);
			
			return p;
		}
		catch(Exception e) { return null; }
	}
	public List<PayModeMaster> GetAllPayMode()
	{
		Query q = new Query(EntityName.PayMode);
		
		return GetPayModeMasterList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	public List<PayModeMaster> GetPayModeMasterList(List<Entity> entities)
	{
		List<PayModeMaster> list = new ArrayList<PayModeMaster>();
		for(Entity e : entities)
		{
			PayModeMaster t = new PayModeMaster();
			t.setEntity(e);
			list.add(t);
		}
		return list;
	}
}
